package com.aimao.blog.constant;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;

/**
 * @Author PuaChen
 * @Create 2018-07-02 16:28
 */
public class ServerResponse<T> implements Serializable {

    public Integer code;
    public String msg;
    public T data;

    private ServerResponse() {
    }

    @JSONField(serialize = false)
    public boolean isSuccess() {
        if (ResponseState.SUCCESS.code.equals(code)) {
            return true;
        }
        return false;
    }

    public static ServerResponse createSuccess() {
        ServerResponse serverResponse = new ServerResponse();
        serverResponse.code = ResponseState.SUCCESS.code;
        serverResponse.msg = ResponseState.SUCCESS.msg;
        return serverResponse;
    }

    public static <T> ServerResponse<T> createSuccessAndData(String msg, T data) {
        ServerResponse serverResponse = new ServerResponse();
        serverResponse.code = ResponseState.SUCCESS.code;
        serverResponse.msg = msg;
        serverResponse.data = data;
        return serverResponse;
    }

    public static <T> ServerResponse<T> createSuccessAndData(T data) {
        ServerResponse serverResponse = new ServerResponse();
        serverResponse.code = ResponseState.SUCCESS.code;
        serverResponse.msg = ResponseState.SUCCESS.msg;
        serverResponse.data = data;
        return serverResponse;
    }

    public static ServerResponse createFailure() {
        ServerResponse serverResponse = new ServerResponse();
        serverResponse.code = ResponseState.FAILURE.code;
        serverResponse.msg = ResponseState.FAILURE.msg;
        return serverResponse;
    }

    public static ServerResponse createFailure(String msg) {
        ServerResponse serverResponse = new ServerResponse();
        serverResponse.code = ResponseState.FAILURE.code;
        serverResponse.msg = msg;
        return serverResponse;
    }


    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
